####################################
# Prepare final clean data
####################################

rm(list=ls())

library(Hmisc)
library(ggplot2)
library(stargazer)
library(foreign)
library(rdrobust)
library(rdd)
library(readstata13)

################
# Prepare data 
################

# reada data
d = read.dta13("~/Dropbox/Anti-american attitudes/01_data/raw_data/LAPOP_five_countries_clean.dta")   
names(d)

# score 
d$election = "2016-11-09"
d$score = as.Date(as.character(d$fecha), format="%Y-%m-%d") - as.Date(as.character(d$election), format="%Y-%m-%d")
d$score[d$score>=0] = d$score[d$score>=0] + 1
d$score = as.numeric(d$score)
d$treatment  = 0
d$treatment[d$score>0]=1
table(d$treatment)
table(d$score)

# prepare outcomes
d$us_trust_bin= 0
d$us_trust_bin[d$mil10e=="Very Trustworthy"]=1
d$us_trust_bin[d$mil10e=="Somewhat Trustworthy"]=1
d$us_trust_bin[d$mil10e=="Not Very Trustworthy"]=0
d$us_trust_bin[d$mil10e=="Untrustworthy"]=0
table(d$us_trust_bin, exclude = NULL)

d$us_trust_con = NA
d$us_trust_con[d$mil10e=="Very Trustworthy"]=4
d$us_trust_con[d$mil10e=="Somewhat Trustworthy"]=3
d$us_trust_con[d$mil10e=="Not Very Trustworthy"]=2
d$us_trust_con[d$mil10e=="Untrustworthy"]=1
table(d$us_trust_con, exclude = NULL)

d$af_trust_bin = 0
d$af_trust_bin[d$b12==5] = 1
d$af_trust_bin[d$b12==6] = 1
d$af_trust_bin[d$b12==7] = 1
table(d$af_trust_bin, exclude = NULL)

d$muni_trust_bin = 0
d$muni_trust_bin[d$b32==5] = 1
d$muni_trust_bin[d$b32==6] = 1
d$muni_trust_bin[d$b32==7] = 1
table(d$muni_trust_bin, exclude = NULL)

# female covariate
table(d$q1)
d$female = 0
d$female[d$q1=="Female"]=1
table(d$female)

# age covariate
table(d$q2y)
d$age = 2016-d$q2y
table(d$age, exclude = NULL)

# education covariate
table(d$ed, exclude = NULL)
d$education = NA
d$education[d$ed==0] = 1
d$education[d$ed>0 & d$ed<6] = 1
d$education[d$ed>0 & d$ed<8 & d$pais==21] = 1
d$education[d$ed==6] = 2
d$education[d$ed==8 & d$pais==21] = 2
d$education[d$ed>6 & d$ed<12] = 3
d$education[d$ed>8 & d$ed<12 & d$pais==21] = 3
d$education[d$ed==12] = 4
d$education[d$ed>12] = 5
table(d$education, exclude = NULL)

# pais
table(d$pais)

d$elsalvador = 0
d$elsalvador[d$pais==3]=1
table(d$elsalvador)

d$honduras = 0
d$honduras[d$pais==4]=1
table(d$honduras)

d$paraguay = 0
d$paraguay[d$pais==12]=1
table(d$paraguay)

d$venezuela = 0
d$venezuela [d$pais==16]=1
table(d$venezuela )

d$dominicanrepublic = 0
d$dominicanrepublic[d$pais==21]=1
table(d$dominicanrepublic)

# cluster
d$cluster = sort(d$cluster)
d$cluster2 = paste(d$cluster,d$pais,sep = "_")
d$cluster2  = as.factor(d$cluster2)
d$cluster2  = as.numeric(d$cluster2)
describe(d$cluster2)

# country
d$country = 0
d$country[d$pais==3] = "El Salvador"
d$country[d$pais==4] = "Honduras"
d$country[d$pais==12] = "Paraguay"
d$country[d$pais==16] = "Venezuela"
d$country[d$pais==21] = "Dominican Republic"
table(d$pais)

# ideology
table(d$l1, exclude = NULL)
d$left = 0
d$left[d$l1==1] = 1
d$left[d$l1==2] = 1
d$left[d$l1==3] = 1
d$left[d$l1==4] = 0
table(d$left)

# left president
d$left_president =  0
d$left_president[d$country=="Venezuela"] = 1
d$left_president[d$country=="El Salvador"] = 1
table(d$left_president)

# south
table(d$pais)
d$south = 0
d$south[d$pais==12]=1
d$south[d$pais==16]=1
table(d$south)

# missingness
describe(d$us_trust_con)
d$missigness = 0
d$missigness[is.na(d$us_trust_con)] = 1
table(d$missigness)

###########################
# Save
###########################

write.dta(d,"~/Dropbox/Anti-american attitudes/01_data/clean_data/trump_election_data_clean_final.dta")
